package controllers

import (
	"errors"
	"go_demo/internal/models"
	"go_demo/internal/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)
// 定义UserController结构体，返回userService
type UserController struct {
	userService *services.UserService
}
// 定义实例化UserController的方法
func NewUserController(userService *services.UserService) *UserController {
	return &UserController{userService: userService}
}
// 创建用户
func (uc *UserController) CreateUser(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := uc.userService.CreateUser(&user); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, user)
}
// 通过id查找用户
func (uc *UserController) GetUserById(c *gin.Context) {
	id := c.Param("id")
	userId, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
		return
	}

	user, err := uc.userService.GetUserByID(uint(userId))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
		return
	}

	c.JSON(http.StatusOK, user)
}
// 查找所有用户
func (uc *UserController) GetAllUser(c *gin.Context) {
	users, err := uc.userService.GetAllUser()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
		return
	}
	c.JSON(http.StatusOK, users)
}
// 通过id删除用户
func (uc *UserController) DeleteUserById(c *gin.Context) {
    idStr := c.Param("id")
    id, err := strconv.Atoi(idStr)
    if err != nil || id <= 0 {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
        return
    }

    if err := uc.userService.DeleteUserById(id); err != nil {
        if errors.Is(err, errors.New("user not found")) {
            c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
            return
        }
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusOK, gin.H{"message": "User deleted successfully"})
}
// 更新用户信息
func (uc *UserController) UpdateUser(c *gin.Context) {
    var updatedUser models.User
    if err := c.ShouldBindJSON(&updatedUser); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    idStr := c.Param("id")
    id, err := strconv.Atoi(idStr)
    if err != nil || id <= 0 {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
        return
    }

    if err := uc.userService.UpdateUser(uint(id), &updatedUser); err != nil {
        if errors.Is(err, errors.New("user not found")) {
            c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
            return
        }
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusOK, gin.H{"message": "User updated successfully", "user": updatedUser})
}
// 用户登录
func(uc *UserController)UserLogin(c *gin.Context){
	name:= c.PostForm("username")
	password:= c.PostForm("password")
	user,err:=uc.userService.UserLogin(name)
	if err!=nil {
		c.JSON(400,gin.H{"error":"没有此用户！！"})
	}
	if password == user.Password{
		c.JSON(200,user)
	}else {
		c.JSON(400,gin.H{"error":"密码不正确"})
	}
}

